package icedot.library.common.base;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;


public class BaseApplication extends Application
{
	protected List<BaseActivity>  mActivityList = null;	
	protected CrashHandler		_crashHandler;      //
	
	@Override
	public void onCreate() 
	{
		super.onCreate();
		mActivityList = new ArrayList<BaseActivity>();
	}
	
	public void setCrashFileName(String name)
	{
		_crashHandler = CrashHandler.getInstance();
		_crashHandler.setFileName(name);
		_crashHandler.init(this);
	}

	public void setCrashHandler(CrashHandler handler)
	{
		_crashHandler = handler;
	}

	
	/**
	 * 添加Activity
	 * @param activity
	 */
	public void addActivity(BaseActivity activity)
	{
		if(activity == null)
		{
			return;
		}
		mActivityList.add(activity);
	}
	
	public void removeAllActivity()
	{
		for(Activity act:mActivityList)
		{
			act.finish();
		}
	}
	
	/**
	 * 移除参数之外的所有activity
	 * @param activity
	 */
	public void removeOtherActivity(BaseActivity activity)
	{
	    for(Activity act:mActivityList)
        {
	        if(activity == act)
	        {
	            continue;
	        }
            act.finish();
        }
	}
	
	/**
	 * 退出程序时调用
	 */
	public void exitApp()
	{
		removeAllActivity();
		System.exit(0);
	}

	/**
	 * 强杀APP
	 */
	public void killApp()
	{
		removeAllActivity();
		int pid = android.os.Process.myPid();
		android.os.Process.killProcess(pid);
	}
	
	/**
	 * 移除Activity
	 * @param activity
	 */
	public void popActivity(Activity activity)
	{
		if(activity == null)
		{
			return;
		}
		
		activity.finish();
		mActivityList.remove(activity);
	}
}
